package org.cainiao.util;

import lombok.experimental.UtilityClass;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <br />
 * <p>
 * Author: Cai Niao(wdhlzd@163.com)<br />
 */
@UtilityClass
public class Util {

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    private static String now() {
        return LocalDateTime.now().format(DATE_TIME_FORMATTER);
    }

    public static void log(String name, Object value) {
        System.out.printf("%s >>> %s >>> %s = %s\n", now(),
            Thread.currentThread().getName(), name, value == null ? "null" : value.toString());
    }

    public static void logLn(String format, Object... args) {
        System.out.printf("%s >>> %s >>> %s\n", now(),
            Thread.currentThread().getName(), String.format(format, args));
    }

    public static void println(String message) {
        System.out.printf("%s --- %s >>> %s%n", now(), Thread.currentThread().getName(), message);
    }

    public static void silentSleep(long millis) {
        long adjustedMillis = millis < 0 ? 0 : millis;
        try {
            Thread.sleep(adjustedMillis);
        } catch (InterruptedException e) {
            println(e.getMessage());
        }
    }
}
